package com.changgou.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.dao.*;
import com.changgou.goods.pojo.*;
import com.changgou.goods.service.SpuService;
import com.changgou.utli.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private CategorybrandMapper categorybrandMapper;

    /**
     * 查询全部列表
     *
     * @return
     */
    @Override
    public List <Spu> findAll() {
        return spuMapper.selectAll ();
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Spu findById(String id) {
        return spuMapper.selectByPrimaryKey (id);
    }


    /**
     * 增加
     *
     * @param goods
     */
    @Override
    public void add(Goods goods) {
        Spu spu = goods.getSpu ();
        long spid = idWorker.nextId ();
        spu.setId (String.valueOf (spid));
        spu.setIsDelete ("0");
        spu.setIsMarketable ("0");
        spu.setIsMarketable ("0");

        spuMapper.insertSelective (spu);

        //添加sku
        this.saveSkuList (goods);
    }




    private void saveSkuList(Goods goods) {
        Spu spu = goods.getSpu ();
        //查对象
        Category category = categoryMapper.selectByPrimaryKey (spu.getCategory3Id ());

        //查品牌
        Brand brand = brandMapper.selectByPrimaryKey (spu.getBrandId ());
        //查关联关系
        Categorybrand categorybrand = new Categorybrand ();
        categorybrand.setBrandid (spu.getBrandId ());
        categorybrand.setCategoryid (spu.getCategory3Id ());
        int count = categorybrandMapper.selectCount (categorybrand);
        //count等0
        if ( count == 0 ) {
            categorybrandMapper.insert (categorybrand);
        }

        List <Sku> skuList = goods.getSkuList ();
        if ( skuList != null ) {
            for (Sku sku : skuList) {
                sku.setId (String.valueOf (idWorker.nextId ()));
                if ( StringUtils.isEmpty (sku.getSpec ()) ) {
                    sku.setSpec ("{}");
                }
                String name = spu.getName ();
                Map <String, String> specMap = JSON.parseObject (sku.getSpec (), Map.class);
                if ( specMap != null && specMap.size () > 0 ) {
                    for (String valeu : specMap.values ()) {
                        name += "" + valeu;
                    }
                }
                sku.setName (name);
                sku.setSpuId (spu.getId ());
                sku.setCreateTime (new Date ());
                sku.setUpdateTime (new Date ());
                sku.setCategoryId (category.getId ());
                sku.setBrandName (brand.getName ());
                skuMapper.insertSelective (sku);

            }
        }
    }


    /**
     * 修改
     *
     * @param goods
     */

    public void update(Goods goods) {
        Spu spu = goods.getSpu ();
        spuMapper.updateByPrimaryKey (spu);
        Example example = new Example (Sku.class);
        Example.Criteria criteria = example.createCriteria ();
        criteria.andEqualTo ("spuId",spu.getId ());
        skuMapper.deleteByExample (example);


        saveSkuList (goods);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        spuMapper.deleteByPrimaryKey (id);
    }


    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List <Spu> findList(Map <String, Object> searchMap) {
        Example example = createExample (searchMap);
        return spuMapper.selectByExample (example);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page <Spu> findPage(int page, int size) {
        PageHelper.startPage (page, size);
        return (Page <Spu>) spuMapper.selectAll ();
    }

    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @Override
    public Page <Spu> findPage(Map <String, Object> searchMap, int page, int size) {
        PageHelper.startPage (page, size);
        Example example = createExample (searchMap);
        return (Page <Spu>) spuMapper.selectByExample (example);
    }



    /**
     * 根据categorybrand表的id查询spu和sku
     *
     * @param id
     * @return
     */
    @Override
    public Goods findGoodsById(String id) {
        Goods goods = new Goods ();
        Spu spu = spuMapper.selectByPrimaryKey (id);
        goods.setSpu (spu);
        Example example = new Example (Sku.class);
        Example.Criteria criteria = example.createCriteria ();
        criteria.andEqualTo ("spuId", id);
        List <Sku> skus = skuMapper.selectByExample (example);
        goods.setSkuList (skus);
        return goods;
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map <String, Object> searchMap) {
        Example example = new Example (Spu.class);
        Example.Criteria criteria = example.createCriteria ();
        if ( searchMap != null ) {
            // 主键
            if ( searchMap.get ("id") != null && !"".equals (searchMap.get ("id")) ) {
                criteria.andEqualTo ("id", searchMap.get ("id"));
            }
            // 货号
            if ( searchMap.get ("sn") != null && !"".equals (searchMap.get ("sn")) ) {
                criteria.andEqualTo ("sn", searchMap.get ("sn"));
            }
            // SPU名
            if ( searchMap.get ("name") != null && !"".equals (searchMap.get ("name")) ) {
                criteria.andLike ("name", "%" + searchMap.get ("name") + "%");
            }
            // 副标题
            if ( searchMap.get ("caption") != null && !"".equals (searchMap.get ("caption")) ) {
                criteria.andLike ("caption", "%" + searchMap.get ("caption") + "%");
            }
            // 图片
            if ( searchMap.get ("image") != null && !"".equals (searchMap.get ("image")) ) {
                criteria.andLike ("image", "%" + searchMap.get ("image") + "%");
            }
            // 图片列表
            if ( searchMap.get ("images") != null && !"".equals (searchMap.get ("images")) ) {
                criteria.andLike ("images", "%" + searchMap.get ("images") + "%");
            }
            // 售后服务
            if ( searchMap.get ("saleService") != null && !"".equals (searchMap.get ("saleService")) ) {
                criteria.andLike ("saleService", "%" + searchMap.get ("saleService") + "%");
            }
            // 介绍
            if ( searchMap.get ("introduction") != null && !"".equals (searchMap.get ("introduction")) ) {
                criteria.andLike ("introduction", "%" + searchMap.get ("introduction") + "%");
            }
            // 规格列表
            if ( searchMap.get ("specItems") != null && !"".equals (searchMap.get ("specItems")) ) {
                criteria.andLike ("specItems", "%" + searchMap.get ("specItems") + "%");
            }
            // 参数列表
            if ( searchMap.get ("paraItems") != null && !"".equals (searchMap.get ("paraItems")) ) {
                criteria.andLike ("paraItems", "%" + searchMap.get ("paraItems") + "%");
            }
            // 是否上架
            if ( searchMap.get ("isMarketable") != null && !"".equals (searchMap.get ("isMarketable")) ) {
                criteria.andEqualTo ("isMarketable", searchMap.get ("isMarketable"));
            }
            // 是否启用规格
            if ( searchMap.get ("isEnableSpec") != null && !"".equals (searchMap.get ("isEnableSpec")) ) {
                criteria.andEqualTo ("isEnableSpec", searchMap.get ("isEnableSpec"));
            }
            // 是否删除
            if ( searchMap.get ("isDelete") != null && !"".equals (searchMap.get ("isDelete")) ) {
                criteria.andEqualTo ("isDelete", searchMap.get ("isDelete"));
            }
            // 审核状态
            if ( searchMap.get ("status") != null && !"".equals (searchMap.get ("status")) ) {
                criteria.andEqualTo ("status", searchMap.get ("status"));
            }

            // 品牌ID
            if ( searchMap.get ("brandId") != null ) {
                criteria.andEqualTo ("brandId", searchMap.get ("brandId"));
            }
            // 一级分类
            if ( searchMap.get ("category1Id") != null ) {
                criteria.andEqualTo ("category1Id", searchMap.get ("category1Id"));
            }
            // 二级分类
            if ( searchMap.get ("category2Id") != null ) {
                criteria.andEqualTo ("category2Id", searchMap.get ("category2Id"));
            }
            // 三级分类
            if ( searchMap.get ("category3Id") != null ) {
                criteria.andEqualTo ("category3Id", searchMap.get ("category3Id"));
            }
            // 模板ID
            if ( searchMap.get ("templateId") != null ) {
                criteria.andEqualTo ("templateId", searchMap.get ("templateId"));
            }
            // 运费模板id
            if ( searchMap.get ("freightId") != null ) {
                criteria.andEqualTo ("freightId", searchMap.get ("freightId"));
            }
            // 销量
            if ( searchMap.get ("saleNum") != null ) {
                criteria.andEqualTo ("saleNum", searchMap.get ("saleNum"));
            }
            // 评论数
            if ( searchMap.get ("commentNum") != null ) {
                criteria.andEqualTo ("commentNum", searchMap.get ("commentNum"));
            }

        }
        return example;
    }

//    @Override
//    public void Skulist(Goods goods) {
//        //获取spu对象
//        Spu spu = new Spu ();
//        //当前日期
//        Date date = new Date ();
//        //获取品牌对象
//        Brand brand = brandMapper.selectByPrimaryKey (spu.getBrandId ());
//        //获取分类对象
//        Category category = categoryMapper.selectByPrimaryKey (spu.getCategory3Id ());
//        //添加分类与品牌之间的关联
//        Categorybrand categorybrand = new Categorybrand ();
//        categorybrand.setCategoryid (spu.getCategory3Id ());
//        categorybrand.setBrandid (spu.getBrandId ());
//        int i = categorybrandMapper.selectCount (categorybrand);
//        if ( i == 0 ) {
//            categorybrandMapper.insert (categorybrand);
//        }
////Sku集合数据；
//        List <Sku> skuList = goods.getSkuList ();
//        if ( skuList != null ) {
//            //for
//            for (Sku sku : skuList) {
////设置主键ID
//                sku.getId (String.valueOf (idWorker.nextId ()));
//                if ( sku.getSpuId () == null || "".equals (sku.getSpec ()) ) {
//                    sku.setSpec ("{}");
//                }
//                String name = sku.getName ();
//                Map <String, String> map = JSON.parseObject (sku.getSpec (), Map.class);
//                if ( map != null && map.size () > 0 ) {
//                    for (String value : map.values ()) {
//                        name += "" + value;
//                    }
//
//                }
//                sku.setName (name);
//                sku.setSpec (spu.getId ());
//                sku.setCreateTime (date);
//                sku.setUpdateTime (date);
//                sku.setCategoryId (category.getId ());
//                sku.setBrandName (brand.getName ());
//                sku.setCategoryName (category.getName ());
//                skuMapper.insertSelective (sku);
//            }
//        }

 //   }

}
